


// #ifdef H5
import SingleLogin from './login_h5.js';
// #endif



// #ifdef MP-WEIXIN
import SingleLogin from './login.js';
// #endif


const $http = async function (url = '', data = {}, method = 'POST',bol) {
  let token;
  //登录
  if (bol) {
    token='';
  } else {
    let _LoginManager = SingleLogin.getInstance();
    token = await _LoginManager.getToken();
  }

  return new Promise(async (resolve, reject) => {

    uni.request({
      url: url,
      data: data,
      method: method,
      header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': token, },
      success: (async res => {
        //活动未上线 活动已下线
        if (res.data.ReturnCode == 300002 || res.data.ReturnCode == 300005) {
          uni.redirectTo({
            url: '../err/public400/index',
          })
          return;
        }
        if (res.data.ReturnCode === 300003) {
          uni.redirectTo({
            url: '../err/notOnline/index',
          })
          return;
        }
        if (res.data.ReturnCode === 300004) {
          uni.redirectTo({
            url: '../err/end/index',
          })
          return;
        }
        if (res.data.ReturnCode == 300007 || res.data.ReturnCode == 300001) {
          let _LoginManager = SingleLogin.getInstance();
          _LoginManager.clearToken();
          await _LoginManager.getToken();
        }
        resolve(res.data);
      }),
      fail: (res => {
        reject(res.data);
        uni.redirectTo({
          url: '../err/public400/public400',
        })
      })
    })
  })
}
export const $get = (url, data) => {
  return $http(url, data, 'GET')
}
export const $getad = (url, data) => {
  return $http(url, data, 'GET',true)
}
export const $post = (url, data) => {
  return $http(url, data, 'POST')
}
